#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''
Created 2011/12/01
@autor: Sérgio Santos
'''

import sqlite3
import csv

class Convert_Csv:
	#Cria base dados e importa ficheiros csv.	
	def __init__(self):
		
		conexao = sqlite3.connect("treta.db")

		c = conexao.cursor()

		c.execute('''create table if not exists PROF (codigo_do_docente integer,nome text,ultimo_grau text, curso text, categoria text, regime_tempo text, ano_lectivo integer, tipo_estabelecimento text, nome_estabelecimento text)''')

		for numberFile in range(0,11):
	
			spamReader = csv.reader(open('reb'+str(numberFile)+'.csv', 'rb'), delimiter=',')	
			print 'reb'+str(numberFile)+'.csv'	
			for row in spamReader:
				stmt ='''insert into PROF (codigo_do_docente ,nome ,ultimo_grau, curso, categoria, regime_tempo, ano_lectivo, tipo_estabelecimento, nome_estabelecimento ) values("{0}", "{1}", "{2}", "{3}", "{4}", "{5}", "{6}", "{7}", "{8}")'''.\
				format(str(row[0]).replace('\"',' '), str(row[1]).replace('\"', ' '), str(row[2]).replace('\"',' '), str(row[3]).replace('\"',' '), str(row[4]).replace('\"',' '), str(row[5]).replace('\"',' '), str(row[6]).replace('\"',' '), str(row[7]).replace('\"',' '), str(row[8]).replace('\"',' '))
    	     			c.execute(stmt)
    	     			pass
			pass
	
		conexao.commit()

		c.execute("SELECT * FROM PROF")

		conexao.commit()

		c.close()

		print "Done"

